<?php
declare (strict_types = 1);

namespace app\middleware;

use app\Request;
use  interfaces\MiddlewareInterface;
/**
 * RequestMiddleware
 * 接到客户端请求，通过该中间件进行一些调整
 */
class RequestMiddleware implements MiddlewareInterface
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle(Request $request, \Closure $next)
    {
        // 为每一个请求增加一个qid
        Request::macro('qid', function () use ($request){
            $name = strtoupper(substr(md5(gethostname()), 12, 8));
            $ip = strtoupper(substr(md5($request->ip()), 14, 4));
            return uniqid(). '-'.$ip.'-'. $name;
        });
        //增加开始请求时间
        Request::macro('request_start_time', function (){
            return microtime(true);
        });
        return  $next($request);
    }
}
